package com.babyte.breakpad;


import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.babyte.breakpad.callback.NativeCrashInfoCallback;
import com.babyte.breakpad.data.CrashInfo;
import com.mhy.crash.HYCrash;
import com.mhy.crash.utils.CrashModel;
import com.mhy.crash.utils.HYCrashUtils;

/**
 * Created By Mahongyin
 * Date    2025/2/8 11:32
 */
public class BreakpadInitProvider extends ContentProvider {
    /**
     * native不理想的话 后续去掉这里吧
     */
    private void initNativeCrash(Context mContext) {
        //发生native异常时:回调异常信息
        BaByteBreakpad.getInstance().initBreakpad(HYCrashUtils.getCrashCachePath(mContext), new NativeCrashInfoCallback() {
            @Override
            public void onCrashInfo(CrashInfo info) {
                String path = info.getPath() != null ? info.getPath() : "not dump minidump file";
                String nativeInfo = info.getNativeInfo();
                String nativeThreadTrack = info.getNativeThreadTrack();
                String jvmThreadTrack = info.getJvmThreadTrack();

                Throwable e = new Throwable(nativeInfo);
                //格式化
                CrashModel model = new CrashModel();
                model.setEx(e);
                model.setTime(System.currentTimeMillis());
                model.setExceptionMsg(e.getMessage());
                model.setLineNumber(0);
                model.setClassName(jvmThreadTrack);
                model.setFileName(path);
                model.setMethodName(nativeThreadTrack);
                model.setExceptionType("native-crash");

                model.setFullException(path + "\n" + nativeInfo + "\n" + nativeThreadTrack + "\n" + jvmThreadTrack);
                model.setVersionCode(HYCrashUtils.getVersionCode(mContext));
                model.setVersionName(HYCrashUtils.getVersionName(mContext));

                HYCrash.getInstance(mContext).showCrashActivity(model);
                //打印日志
                BaByteBreakpad.getInstance().formatPrint("HYCrash", info);
            }
        });

    }

    @Override
    public boolean onCreate() {
        Context context = getContext();
        if (context == null) {
            context = HYCrashUtils.getApplicationByReflect();
        } else if (context.getApplicationInfo() != null){
            context = context.getApplicationContext();
        }
        initNativeCrash(context);
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        return null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return "";
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }
}
